package com.basketball.gym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basketball.gym.mapper.FinanceMapper;
import com.basketball.gym.mapper.UserMapper;
import com.basketball.gym.pojo.Finance;
import com.basketball.gym.pojo.ResponseData;
import com.basketball.gym.pojo.User;
import com.basketball.gym.pojo.Wallet;
import com.basketball.gym.mapper.WalletMapper;
import com.basketball.gym.pojo.state.ResponseState;
import com.basketball.gym.service.WalletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basketball.gym.utils.StringUtils;
import com.basketball.gym.utils.WrapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 赵肖云
 * @since 2021-03-27
 */
@Validated
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet> implements WalletService {
    @Autowired
    private WalletMapper walletMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FinanceMapper financeMapper;

    /**
     * 查询钱包
     */
    @Override
    public ResponseData<Wallet> getWallet(User user) {
        Wallet wallet = walletMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("user_id", user.getId()));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), wallet);
    }

    /**
     * 管理员通过手机号充值
     */
    @Override
    @Transactional
    public ResponseData<Wallet> topUp(@NotNull(message = "手机号不能为空") String phone, @Min(value = 0, message = "price不能小于0") @NotNull(message = "price不能为空") Double price) {
        User user = userMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("phone", phone));
        if (user == null) {
            return new ResponseData<>(ResponseState.USER_NOT_EXIST.getMessage(), ResponseState.USER_NOT_EXIST.getValue());
        }
        //计算额外的悦动币
        Double extra = 0.0;
        if (price >= 10000) {
            extra = 1500.0;
        } else if (price > 500) {
            extra = 500.0;
        } else if (price >= 2000) {
            extra = 100.0;
        }
        Wallet wallet = walletMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("user_id", user.getId()));
        wallet.setBalance(StringUtils.sum(StringUtils.sum(wallet.getBalance(),price),extra));
        walletMapper.updateById(wallet);
        financeMapper.insert(new Finance("前台充值" + price + ",额外奖励" + extra, 1, 1, price + extra, user.getId()));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 消耗入场券
     */
    @Override
    public ResponseData<Wallet> useTicket(@NotNull(message = "手机号不能为空") String phone, @Min(value = 0, message = "ticketNum不能小于0") @NotNull(message = "ticketNum不能为空") Integer ticketNum) {
        User user = userMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("phone", phone));
        if (user == null) {
            return new ResponseData<>(ResponseState.USER_NOT_EXIST.getMessage(), ResponseState.USER_NOT_EXIST.getValue());
        }
        Wallet wallet = walletMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("user_id", user.getId()));
        if (wallet.getTicket() < ticketNum) {
            return new ResponseData<>(ResponseState.BALANCE_NOT_ENOUGH.getMessage(), ResponseState.BALANCE_NOT_ENOUGH.getValue());
        }
        wallet.setTicket(wallet.getTicket() - ticketNum);
        walletMapper.updateById(wallet);
        financeMapper.insert(new Finance("使用入场券", 3, 2, ticketNum * 1.0, user.getId()));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 使用余额
     */
    @Override
    public ResponseData<Object> useBalance(Finance finance) {
        User user=userMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("phone",finance.getPhone()));
        if (user == null) {
            return new ResponseData<>(ResponseState.USER_NOT_EXIST.getMessage(), ResponseState.USER_NOT_EXIST.getValue());
        }
        //判断余额是否充足
        Wallet wallet = walletMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("user_id", user.getId()));
        if (wallet.getBalance() < finance.getSum()) {
            return new ResponseData<>(ResponseState.BALANCE_NOT_ENOUGH.getMessage(), ResponseState.BALANCE_NOT_ENOUGH.getValue());
        }

        wallet.setBalance(StringUtils.sub(wallet.getBalance(),finance.getSum()));
        wallet.setIntegration(StringUtils.sum(wallet.getIntegration(),finance.getSum()));
        finance.setType(1);
        finance.setPaymentType(2);
        finance.setUserId(user.getId());
        walletMapper.updateById(wallet);
        financeMapper.insert(finance);
        financeMapper.insert(new Finance("店内消费",2,1,finance.getSum(),user.getId()));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 通过手机号查询钱包
     */
    @Override
    public ResponseData<Wallet> getWalletByPhone(String phone) {
        User user=userMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("phone",phone));
        if (user == null) {
            return new ResponseData<>(ResponseState.USER_NOT_EXIST.getMessage(), ResponseState.USER_NOT_EXIST.getValue());
        }
        //判断余额是否充足
        Wallet wallet = walletMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("user_id", user.getId()));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),wallet);
    }

    /**
     * 管理员查询钱包
     */
    @Override
    public ResponseData<List<Wallet>> adminGetWallet(String phone) {
        List<Wallet> walletList=null;
        if(phone!=null){
            User user=userMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("phone",phone));
            if (user == null) {
                return new ResponseData<>(ResponseState.SUCCESS.getMessage(),ResponseState.SUCCESS.getValue());
            }else {
                walletList=walletMapper.selectList(WrapperUtils.getOneEqQueryWrapper("user_id",user.getId()));
            }
        }else {
            walletList=walletMapper.selectList(new QueryWrapper<>());
        }
        for(Wallet wallet:walletList){
            wallet.setUser(userMapper.selectById(wallet.getUserId()));
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),walletList);
    }

}
